package mall.repository;

import mall.entity.Order;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class OrderDao extends BaseDao<Order> {
    @Override
    public Order rowToObject(ResultSet rs) throws Exception {
        Order order = new Order();
        order.setId(rs.getInt("id"));
        order.setNumber(rs.getString("number"));
        order.setName(rs.getString("name"));
        order.setPhone(rs.getString("phone"));
        order.setAddress(rs.getString("address"));
        order.setUserId(rs.getInt("user_id"));
        order.setTotalPrice(rs.getBigDecimal("total_price"));
        order.setPayTime(rs.getDate("pay_time"));
        order.setOrderTime(rs.getDate("order_time"));
        order.setStatus(rs.getInt("status"));
        return order;
    }


    public int insert(Order order) {
        String sql = "insert into `order`(number, user_id,name,phone,address, total_price ,order_time) values(?, ?, ?, ?, ?, ?, ?)";
        return executeUpdate(sql, order.getNumber(), order.getUserId(), order.getName(), order.getPhone(), order.getAddress(), order.getTotalPrice(), new Date());
    }

    public List<Order> selectListByUserId(Integer userId) {
        return executeQuery("select * from  `order` where user_id= ? order by order_time desc ", userId);
    }

    public Order selectById(Integer orderId) {
        return executeQuery("select * from  `order` where id= ? ", orderId).get(0);
    }

    public int updateStatus(Integer orderId, int status) {
        if (status == 1) {
            String sql = "update `order` set status = ?, pay_time = ?  where id = ?";
            return executeUpdate(sql, status, new Date(), orderId);
        } else {
            String sql = "update `order` set status = ? where id = ?";
            return executeUpdate(sql, status, orderId);
        }


    }

    public List<Order> selectList(Order order) {
        if (order.getNumber() != null && !Objects.equals(order.getNumber(), "")) {
            return executeQuery("select * from  `order` where number = ? ", order.getNumber());
        } else {
            return executeQuery("select * from  `order` ");
        }

    }

    public Integer count(Order order) {
        StringBuilder sb = new StringBuilder("select count(*) from  `order` where 1=1 ");
        List<Object> params = new ArrayList<>();

        if (order.getId() != null) {
            sb.append(" and id = ?");
            params.add(order.getId());
        }
        if (order.getNumber() != null && !Objects.equals(order.getNumber(), "")) {
            sb.append(" and number = ?");
            params.add(order.getNumber());
        }
        return executeQueryCount(sb.toString(), params.toArray());
    }

    public List<Order> selectList(Order order, Integer offset, Integer size) {
        StringBuilder sb = new StringBuilder("select * from  `order` where 1=1 ");
        List<Object> params = new ArrayList<>();

        if (order.getId() != null && order.getId() != 0) {
            sb.append(" and id = ?");
            params.add(order.getId());
        }

        if (order.getNumber() != null && !Objects.equals(order.getNumber(), "")) {
            sb.append(" and number = ?");
            params.add(order.getNumber());
        }

        sb.append(" limit ?, ?");
        params.add(offset);
        params.add(size);

        return executeQuery(sb.toString(), params.toArray());
    }
}
